#ifndef _COPYOBJECT_HPP
#define _COPYOBJECT_HPP

#include "Allocate.hpp"

namespace builtin {
	Object* copyObject(Object* oldObject) {
		if (oldObject == nullptr) return nullptr;
		if (typeid(Integer) == typeid(*oldObject)) {
			Integer* object = dynamic_cast<Integer*>(oldObject);
			Integer* copyObject = new Integer(*object);
			return copyObject;
		}
		else if (typeid(StringObject) == typeid(*oldObject)) {
			StringObject* object = dynamic_cast<StringObject*>(oldObject);
			StringObject* copyObject = new StringObject(*object);
			return copyObject;
		}
		else if (typeid(FloatObject) == typeid(*oldObject)) {
			FloatObject* object = dynamic_cast<FloatObject*>(oldObject);
			FloatObject* copyObject = new FloatObject(*object);
			return copyObject;
		}
		else if (typeid(Boolean) == typeid(*oldObject)) {
			Boolean* object = dynamic_cast<Boolean*>(oldObject);
			Boolean* copyObject = new Boolean(*object);
			return copyObject;
		}
		else if (typeid(Error) == typeid(*oldObject)) {
			Error* object = dynamic_cast<Error*>(oldObject);
			Error* copyObject = new Error(*object);
			return copyObject;
		}
		else if (typeid(NullObject) == typeid(*oldObject)) {
			NullObject* object = dynamic_cast<NullObject*>(oldObject);
			NullObject* copyObject = new NullObject();
			return copyObject;
		}
		else if (typeid(ArrayObject) == typeid(*oldObject)) {
			MemoryMgr::getInstance().addRef(oldObject);
			return oldObject;
		}
		else if (typeid(FunctionObject) == typeid(*oldObject)) {
			MemoryMgr::getInstance().addRef(oldObject);
			return oldObject;
		}
		else if (oldObject->Type() == STRUCT_OBJECT) {
			MemoryMgr::getInstance().addRef(oldObject);
			return oldObject;
		}
		else if (oldObject->Type() == CLASS_OBJECT) {
			MemoryMgr::getInstance().addRef(oldObject);
			return oldObject;
		}
		else {
			return nullptr;
		}
	}
}

#endif // !_COPYOBJECT_HPP
